﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using BEGDC.CC.Web.UserControls;
using BEGDC.CC.BL.CCFlow.CR;
using BEGDC.CC.Entity;
using BEGDC.CC.BL.Workflow;
using BEGDC.CC.BL.Common;
using BEGDC.Utility.Castle;


namespace BEGDC.CC.Web.Common
{
    /// <summary>
    /// Summary description for VehiclePopUp.
    /// </summary>
    public class VehiclePopUp : System.Web.UI.Page
    {
        #region VARIABLES
        protected System.Web.UI.WebControls.TextBox Ctl_VehicleName;
        protected System.Web.UI.WebControls.DropDownList Dp_Type;
        protected System.Web.UI.WebControls.Button Btn_Save;
        protected System.Web.UI.WebControls.Label lblTitle;
        protected System.Web.UI.WebControls.Button Btn_Delete;
        protected System.Web.UI.WebControls.TextBox Ctl_Price;
        #endregion

        private void Page_Load(object sender, System.EventArgs e)
        {
            string ItemIDStr = Request["ItemID"];
            //string StateStr = Request["State"];

            if (ItemIDStr != null)
            {
                this.ItemID = Convert.ToInt32(ItemIDStr);
            }
            else
            {
                this.ItemID = 0;
            }

            if (!this.IsPostBack)
            {
                this.Btn_Save.Attributes["onclick"] = "javascript:OnSave();";
                this.Btn_Delete.Attributes["onclick"] = "javascript:OnDelete();";

                //WebUtility.BuildVehicleType(this.Dp_VehicleType,BEGDC.CC.BL.Common.DicType.AlcatelCar);

                if (this.ItemID > 0)
                {
                    this.Btn_Delete.Visible = true;
                    InitForm();
                    this.lblTitle.Text = "Edit Vehicle (编辑车辆)";
                }
                else
                {
                    this.lblTitle.Text = "Add Vehicle (添加车辆)";
                    this.Btn_Delete.Visible = false;
                   
                }

                //				if(this.Dp_Type.SelectedValue==BEGDC.CC.BL.Common.DicType.AlcatelCar)
                //				{
                //					this.Ctl_Price.Text = "0";
                //					this.Ctl_Price.Enabled = false;
                //				}
            }
        }

        #region ItemID
        protected int ItemID
        {
            get { return (int)this.ViewState["VehicleItemID"]; }
            set { this.ViewState["VehicleItemID"] = value; }
        }
        #endregion

        #region InitForm
        private void InitForm()
        {
            ICR CRService = AppContainer.Instance[ServiceConst.CRService] as ICR;
            BEGDC.CC.Entity.VehicleType vehicle = CRService.GetVehicleType(this.ItemID);

            this.Ctl_VehicleName.Text = vehicle.VehicleTypeName;
            this.Dp_Type.SelectedIndex = Dp_Type.Items.IndexOf(Dp_Type.Items.FindByValue(vehicle.VehicleSource));
            this.Ctl_Price.Text = vehicle.Price.ToString();

            //			if(this.Dp_Type.SelectedItem!=null&&this.Dp_Type.SelectedValue!=BEGDC.CC.BL.Common.DicType.AlcatelCar)
            //			{
            //				this.Ctl_Price.Enabled = true;
            //			}
            //			else
            //			{
            //				this.Ctl_Price.Text = "0";
            //				this.Ctl_Price.Enabled = false;
            //			}
        }
        #endregion

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Btn_Save.Click += new System.EventHandler(this.Btn_Save_Click);
            this.Btn_Delete.Click += new System.EventHandler(this.Btn_Delete_Click);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

        private void Btn_Save_Click(object sender, EventArgs e)
        {
            try
            {
                ICR CRService = AppContainer.Instance[ServiceConst.CRService] as ICR;
                BEGDC.CC.Entity.VehicleType vehicle = null;
                if (this.ItemID > 0)
                {
                    vehicle = CRService.GetVehicleType(this.ItemID);
                }
                else
                {
                    vehicle = new BEGDC.CC.Entity.VehicleType();
                }
                vehicle.VehicleTypeName = this.Ctl_VehicleName.Text;
                vehicle.VehicleSource = this.Dp_Type.SelectedValue;
                vehicle.Price = Convert.ToDecimal(this.Ctl_Price.Text);
                CRService.UpdateVehicles(vehicle, false);
            }
            catch (Exception exp)
            {
                throw exp;
            }
            Response.Write("<script>window.close();window.returnValue='reload';</script>");
        }

        private void Btn_Delete_Click(object sender, EventArgs e)
        {
            try
            {
                ICR CRService = AppContainer.Instance[ServiceConst.CRService] as ICR;
                BEGDC.CC.Entity.VehicleType vehicle = null;
                if (this.ItemID > 0)
                {
                    vehicle = CRService.GetVehicleType(this.ItemID);
                    CRService.UpdateVehicles(vehicle, true);
                }
            }
            catch (Exception exp)
            {
                throw exp;
            }
            Response.Write("<script>window.close();window.returnValue='reload';</script>");
        }

        //		private void Dp_Type_SelectedIndexChanged(object sender, System.EventArgs e)
        //		{
        //			if(this.Dp_Type.SelectedValue==BEGDC.CC.BL.Common.DicType.AlcatelCar)
        //			{
        //				this.Ctl_Price.Text = "0";
        //				this.Ctl_Price.Enabled = false;
        //			}
        //			else
        //			{
        //				this.Ctl_Price.Enabled = true;
        //			}
        //		}
    }
}
